package com.atguigu.paymentdemo.service.impl;

import com.atguigu.paymentdemo.entity.OrderInfo;
import com.atguigu.paymentdemo.entity.RefundInfo;
import com.atguigu.paymentdemo.enums.OrderStatus;
import com.atguigu.paymentdemo.mapper.RefundInfoMapper;
import com.atguigu.paymentdemo.service.OrderInfoService;
import com.atguigu.paymentdemo.service.RefundInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.refund.model.RefundNotification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {
    @Autowired
    private OrderInfoService orderInfoService;

    @Override
    public void insertByInfo(RefundNotification refundNotification) {
        // 根据订单号插入数据
        OrderInfo one = orderInfoService.lambdaQuery().eq(OrderInfo::getOrderNo, refundNotification.getOutTradeNo()).one();
        if (one != null && one.getOrderStatus().equals(OrderStatus.REFUND_SUCCESS)) {
            return;
        }

        RefundInfo refundInfo = new RefundInfo();
        refundInfo.setRefundNo(refundNotification.getOutRefundNo());
        refundInfo.setOrderNo(refundNotification.getOutTradeNo());
        refundInfo.setRefundId(refundNotification.getRefundId());
        refundInfo.setTotalFee(Math.toIntExact(refundNotification.getAmount().getTotal()));
        refundInfo.setRefund(Math.toIntExact(refundNotification.getAmount().getRefund()));
        refundInfo.setReason("正常退款");
        refundInfo.setRefundStatus(refundNotification.getRefundStatus().toString());
        save(refundInfo);
        
    }
}
